<?
include "../db_config.php";
include "Database.class.php";

$db = new Database($host_name, $user_name, $db_password, $db_name);
$db->connect();


$lat = $HTTP_GET_VARS['lat'];
$lng = $HTTP_GET_VARS['lng'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Busnow made by Apolo team</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="./XHConn.js"></script>


<style type="text/css">
#waiting{
	position:absolute;
	top:300px;
	left:300px;
	visibility:hidden;
	background-color:white;
	z-index:100;
}
#totalNum{
	position:absolute;
	top:5px;
	left:5px;
}
#infoWin{
	position:absolute;
	top:0px;
	left:100px;
	background-color:white;
	margin:0px 10px 10px 10px;
	z-index:100;
}
#infoWin2{
	position:absolute;
	top:0px;
	left:400px;
	background-color:white;
	margin:0px 10px 10px 10px;
	z-index:100;
}

#refreshbtn{
	position:absolute;
	top:100px;
	left:500px;
	width:100px;
	height:50px;
	background-color:white;
	z-index:100;
}
#clock{
	position:absolute;
	top:100px;
	left:400px;
	background-color:white;
	z-index:100;
}
.distance{
	background-color:white;
}


</style>

<script type="text/javascript">
var g_line = "";
var mapWidth = '100%';
var mapHeight = '100%';
var mapObj;
var center;
var markersArray = [];
function setCurrentPos(position) {
	center = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
}


function initialize()
{
	center = new google.maps.LatLng(37.3595504, 127.1056093 );
<?
if($lat != null && $lng != null)
echo "center = new google.maps.LatLng($lat, $lng);\n";
else
echo "navigator.geolocation.getCurrentPosition(setCurrentPos);";
?>
	var myOptions = {
		zoom:14,
		center:center,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	mapObj = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	//mapObj.setZoom(14);
	//mapObj.setUIToDefault();

	getBusInfo();

	google.maps.event.addListener(mapObj, "click", getBusInfo);
	google.maps.event.addListener(mapObj, "dragend", getBusInfo);
	google.maps.event.addListener(mapObj, "zoom_changed", getBusInfo);

	check();
	setInterval("refresh()", 1000);
	//window.setTimeout("calculate()", 1000);
}
function calculate()
{
	var xmlhttp = GXmlHttp.create();
	var url = "calculateTrainPos.php";
	xmlhttp.open('GET', url, true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState==4)
		{
			var d= xmlhttp.responseText;
			document.getElementById("data").innerHTML = d;
			window.setTimeout("calculate()", d);
		}
	}
	xmlhttp.send(null);

}

function refresh()
{
	if(refreshState == 1)
		getBusInfo();
}

function addBusIcon(x, y, remain, bus_no, type)
{
	var n1, n2;

	var base_img = "./img/";
	n1 = new google.maps.LatLng(x, y);
	var image_url = base_img + type + '.png';
	var image = new google.maps.MarkerImage(image_url,
	new google.maps.Size(20,60),
	new google.maps.Point(0, 0),
	new google.maps.Point(0, 10));

	var icon = new google.maps.Marker({
		position:n1,
		map:mapObj,
		icon:image
	});
	icon.setMap(mapObj);
	markersArray.push(icon);
	var infowindow = new google.maps.InfoWindow({
		content:bus_no,
		position:n1
	});
	infowindow.open(mapObj);

	/*
	icon = new GIcon(G_DEFAULT_ICON);
	icon.shadow = base_img + type+".png";
	icon.shadowSize = new GSize(20, 60);
	icon.image= base_img + "stop.png";
	icon.iconSize = new GSize(40, 20);
	*/


	//역 정차 상태
	/*
	if(remain <= -10)
	{
		icon = new GIcon(G_DEFAULT_ICON);
		icon.shadow = base_img + "sw"+line+".gif";
		icon.shadowSize = new GSize(40, 48);
		icon.image= base_img + "stop.png";
		icon.iconSize = new GSize(40, 20);
	}
	else if(remain < 0)
	{
		//출발전 카운트 상태
		var j = -1*remain;
		icon = new GIcon(G_DEFAULT_ICON);
		icon.shadow = base_img + "sw"+line+".gif";
		icon.shadowSize = new GSize(40, 48);
		icon.image= base_img + j + ".gif";
		icon.iconSize = new GSize(20, 20);
	}
	else
	{
		//움직이는 중
		icon = new GIcon(G_DEFAULT_ICON);
		icon.image = base_img + "sw"+line+".gif";
		icon.iconSize = new GSize(40, 48);
	}
	*/
	
//	var s = "남은 시간 : " + remain + "초\n"+line+"호선 "+updown_s;

//	markerOptions = {title:s, icon:icon};
	
	/*
	var s= remain + "분";
	markerOptions = {title:s, icon:icon};
	markerOptions = {icon:icon};
	mapObj.addOverlay(new GMarker(n1, markerOptions));
	return;
	*/

}
function showList2(list)
{
	document.getElementById("infoWin2").innerHTML = list;
}
function showList(list)
{
	document.getElementById("infoWin").innerHTML = list;
}
function hideList()
{
	//document.getElementById("infoWin").style.visibility = "hidden";
	document.getElementById("infoWin").innerHTML = "";
}

function getAPRawData(bssid)
{
	var xmlhttp = GXmlHttp.create();
	//xmlhttp.setType(0);
	var url = "getAPRawData.php?bssid="+bssid;
	xmlhttp.open('GET', url, true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
			showAPRawData(xmlhttp.responseText);
	}
	xmlhttp.send(null);
}
	
function showAPRawData(res)
{
	//window.console.log(str);
	//window.console.log("showAPRawData");
	eval("var s = " + res +";");
	//eval(str);
	document.getElementById('totalNum').innerHTML = "총 AP : "+s.totalNum + "개";
	mapObj.clearOverlays();

	//boundary를 구하기 위해
	var bounds = new GLatLngBounds();

	for(var i in s.Points)
	{
		var node = s.Points[i];
		var p = new GLatLng(node.lat, node.lng);
		bounds.extend(p);
		addBusIcon(node.lat, node.lng, 0, node.SNR+"|"+node.Sig, 1);
	}
	//var b = line.getBound();
	//mapObj.setBound(b[0], b[1], b[2], b[3]);
	mapObj.setCenter(bounds.getCenter(),mapObj.getBoundsZoomLevel(bounds));

	document.getElementById('waiting').style.visibility = "hidden";

}

function clearOverlays() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
  }
  markersArray.length=0;
}

function showBusInfo(res)
{
	//window.console.log(res);
	eval("var s = " + res +";");
	document.getElementById('totalNum').innerHTML = "Current Train no in this map : "+s.totalNum + "";
	clearOverlays();
	for(var i in s.Points)
	{
		var node = s.Points[i];
		addBusIcon(node.lat, node.lng, node.remain_min, node.bus_no, node.bus_type);
	}
	document.getElementById('waiting').style.visibility = "hidden";
}
//ruler 관련 코드
var rulerStatus = 0;
//최소 AP 개수
var minAPNum = 1;

var pt1, pt2;
function changeMinAPNum()
{
	var obj = document.getElementById('minAPNum');
	minAPNum = obj.value;

	getBusInfo();
}
function getBusInfo()
{
	//window.console.log("getBusInfo");
	var b = mapObj.getBounds();
	if (b == undefined ) 
		return;


	//ruler가 동작 중일 때는 동작 안함
	if(rulerStatus != 0)
		return;
	//var lt = mapObj.fromTM128ToLatLng(new NTM128(b[0], b[1]));
	//var rb = mapObj.fromTM128ToLatLng(new NTM128(b[2], b[3]));
	//window.console.log(b);
	var lt = b.getSouthWest();
	var rb = b.getNorthEast();
	var obj = document.getElementById('waiting');
	//obj.style.visibility = "visible";
	var xmlhttp = new XHConn(); 
	var url = "getTrainInfo.php?lat2="+rb.lat()+"&lng1="+lt.lng();
	url = url + "&lat1="+lt.lat() +"&lng2="+rb.lng();
	//var url = "getTrainInfo.php?";
	//window.console.log(url);
	xmlhttp.connect(url, 'GET', '', showBusInfo);
	/*
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		//window.console.log(xmlhttp.responseText);
			showBusInfo(xmlhttp.responseText);
	}
	xmlhttp.send(null);
	*/
}
var logSelect = 1;
function loadLog()
{
	var obj = document.getElementById('log');
	logSelect = obj.value;
	getBusInfo();
}

var refreshState = 1;
function changeRefreshState()
{
	btn = document.getElementById('refreshbtn');
	if(refreshState == 1)
	{
		refreshState = 0;
		btn.innerHTML = 'Auto Update Off';
	}
	else
	{
		refreshState = 1;
		btn.innerHTML = 'Auto Update On';
	}

}
function check()
{
	/*
	var line = document.line;
	var str="";
	for(i = 0; i< line.lineSelect.length; i++)
	{
		if(line.lineSelect[i].checked == true)
		{
			str += line.lineSelect[i].value + "|";
			//alert(chk[i].value);
			//window.console.log(line.lineSelect[i].value);
		}
	}
	//window.console.log(str);
	g_line = str;
	*/
}
function checkAll(t)
{

	var line = document.line;
	var lineSelectAll = document.line.lineSelectAll[t];
	//window.console.log(lineSelectAll.checked);
	var i = t;
	for(; i< line.lineSelect.length; i+=2)
	{
		line.lineSelect[i].checked = lineSelectAll.checked;
		//window.console.log(line.lineSelect[i].checked);
	}
	check();
}

</script>

</head>
<body onload="initialize()">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
For more information, please check the website <a href=http://chaehyun.kr/w/KSubway
target=_blank>http://chaehyun.kr/w/KSubway</a>
<div id=clock>
<embed height="31" width="88" align="middle" pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" allowscriptaccess="sameDomain" bgcolor="#ffffff" wmode="transparent" quality="high"
src="onlineclock.swf?ccolor=black">
</div>
<div id=waiting><img src=./img/wait.gif></div>
<div id="map_canvas" style="width:100%; height:800px"></div>
<div id="display"></div>
<div id="infoWin"></div>
<div id="infoWin2"></div>
<div id=data></div>
<button id=refreshbtn onclick=changeRefreshState()>Auto Update On</button>
<!--
<button onclick=addClick()>click event등록</button>
<button onclick=removeClick()>click event제거</button>
-->
<div id=totalNum></div>
<a href=http://uwa64-023.cafe24.com/WebMysql/ target=_blank>Web MySQL</a>
</body>
</html>

<?
$db->close();
?>
				
